home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / GopherTools / jughead / jughead.0.9 / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-15  |  11.6 KB  |  367 lines

  1. /*****************************************************************************
  2.  * File:    utils.c
  3.  *
  4.  * Author:    Rhett "Jonzy" Jones
  5.  *        jonzy@cc.utah.edu
  6.  *
  7.  * Date:    March 6, 1993
  8.  *
  9.  * Modified:    March 20, 1993, by Rhett "Jonzy" Jones.
  10.  *        Cleaned up some of the routines.
  11.  *
  12.  *        March 28, 1993, by Rhett "Jonzy" Jones.
  13.  *        Added support for varargs.h, which is needed on a sun
  14.  *        and included modifying DoSystemCall().
  15.  *
  16.  *        March 28, 1993, by Rhett "Jonzy" Jones.
  17.  *        Fixed a problem using vsprintf() compiled on a sun.
  18.  *
  19.  *        March 30, 1993, by Rhett "Jonzy" Jones.
  20.  *        Rewrote DoSystemCall() and added Mysprint().
  21.  *
  22.  *        April 4, 1993, by Rhett "Jonzy" Jones.
  23.  *        Added the routine NumberOfLines().
  24.  *
  25.  * Description:    Contains various utlity routines.  See "Routines" for more
  26.  *        information.
  27.  *
  28.  * Routines:    char    *StrToLower(char *str);
  29.  *        long    GetLong(FILE *fp);
  30.  *        int    GetInt(FILE *fp);
  31.  *        char    *GetStr(FILE *fp,char *str,int limit);
  32.  *        int    Str2Int(char *s);
  33.  *        char    *Reverse(char *s, char *r);
  34.  *        short    StrRcmp(char *s1, char *s2);
  35.  *        char    *Mysprint(va_dcl va_alist | char *format,...);
  36.  *        int    DoSystemCall(char *command);
  37.  *        char    *MyStrTok(char *str,char delimeter);
  38.  *        char    *OnlyDigits(char *s);
  39.  *        char    *RemoveCRLF(char *line);
  40.  *
  41.  * Bugs:    No known bugs.
  42.  *
  43.  * Copyright:    Copyright 1993, University of Utah Computer Center.
  44.  *        This source may be freely distributed as long as this copyright
  45.  *         notice remains intact, and is in no way used for any monetary
  46.  *         gain, by any institution, business, person, or persons.
  47.  *
  48.  ****************************************************************************/
  49.  
  50. #ifdef USE_VARARGS_H
  51. #    include <varargs.h>
  52. #else
  53. #    include <stdarg.h>
  54. #endif
  55.  
  56. #include <stdio.h>
  57.  
  58. /*****************************************************************************
  59.  * NumberOfLines returns the number of lines in the file "fileName".
  60.  ****************************************************************************/
  61. long NumberOfLines(fileName)
  62.     char    *fileName;    /* The name of the file to count lines in. */
  63. {    FILE    *fp;        /* Pointer the the file 'fileName'. */
  64.     long    numLines = 0;    /* The number of lines to return. */
  65.     int    c;
  66.  
  67.     if (fp = fopen(fileName,"r"))
  68.         {
  69.         while ((c = fgetc(fp)) != EOF)
  70.             if (c == '\n')
  71.                 numLines++;
  72.         fclose(fp);
  73.         }
  74.     else
  75.         fprintf(stderr,"error: NumberOfLines could not open %s\n",fileName);
  76.     return(numLines);
  77.  
  78. }    /* NumberOfLines */
  79.  
  80. /*****************************************************************************
  81.  * StrToLower returns the string 'str' passed in, which gets converted to
  82.  *lower case.
  83.  ****************************************************************************/
  84. char *StrToLower(str)
  85.     char    *str;    /* The string we are converted to lower case. */
  86. {    char    *s;    /* Pointer to the position we are converting. */
  87.  
  88.     for (s = str; *s; s++)
  89.         *s = tolower(*s);
  90.     return(str);
  91.  
  92. }    /* StrToLower */
  93.  
  94. /*****************************************************************************
  95.  * GetLong returns a long acquired from the stream 'fp'.  This routine
  96.  * supports a '+' or '-' as input as the first character to set the sign.
  97.  * This routine was written because of the problems with fscanf().
  98.  ****************************************************************************/
  99. long GetLong(fp)
  100.     FILE    *fp;        /* The stream we are getting input from. */
  101. {    long    i;        /* The value we return. */
  102.     int    c,        /* A character from the stream. */
  103.         sign = 1;    /* Initial sign of the result. */
  104.  
  105.     while (isspace(c = fgetc(fp)));        /* Skip any whitespace. */
  106.     if (c == '+' || c == '-')        /* Check the sign. */
  107.         {
  108.         sign = (c == '+') ? 1 : -1;
  109.         c = fgetc(fp);
  110.         }
  111.     for (i = 0; isdigit(c); c = fgetc(fp))    /* Get the number. */
  112.         i = i * 10 + c - '0';
  113. #if(0)
  114.     if (c != EOF)                /* Put the character back. */
  115.         ungetc(c,fp);
  116. #endif
  117.     return(i * sign);
  118.  
  119. }    /* GetLong */
  120.  
  121. /*****************************************************************************
  122.  * GetInt returns an int acquired from the stream 'fp'.  This routine
  123. * supports a '+' or '-' as input as the first character to set the sign.
  124.  * This routine was written because of the problems with fscanf().
  125.  ****************************************************************************/
  126. int GetInt(fp)
  127.     FILE    *fp;        /* The stream we are getting input from. */
  128. {    long    i;        /* The value we return. */
  129.     int    c,        /* A character from the stream. */
  130.         sign = 1;    /* Initial sign of the result. */
  131.  
  132.     while (isspace(c = fgetc(fp)));        /* Skip any whitespace. */
  133.     if (c == '+' || c == '-')        /* Check the sign. */
  134.         {
  135.         sign = (c == '+') ? 1 : -1;
  136.         c = fgetc(fp);
  137.         }
  138.     for (i = 0; isdigit(c); c = fgetc(fp))    /* Get the number. */
  139.         i = i * 10 + c - '0';
  140. #if(0)
  141.     if (c != EOF)                /* Put the character back. */
  142.         ungetc(c,fp);
  143. #endif
  144.     return(i * sign);
  145.  
  146.  
  147. }    /* GetInt */
  148.  
  149. /*****************************************************************************
  150.  * GetStr returns a string acquired from the stream 'fp'.
  151.  * This routine was written because of the problems with fscanf().
  152.  ****************************************************************************/
  153. char *GetStr(fp,str,limit)
  154.     FILE        *fp;        /* The stream we are getting input from. */
  155.     char        *str;        /* Where to place the characters. */
  156.     short        limit;        /* Max chars to get. */
  157. {    int        i;        /* A loop counter. */
  158.     int        c;        /* A character from the stream. */
  159.  
  160.  
  161.     while (isspace(c = fgetc(fp)));        /* Skip any whitespace. */
  162.     for (i = 0; i < limit && !isspace(c); c = fgetc(fp))
  163.         str[i++] = (char)c;
  164.     str[i] = '\0';
  165. #if(0)
  166.     if (c != EOF)                /* Put the character back. */
  167.         ungetc(c,fp);
  168. #endif
  169.     return(str);
  170.  
  171. }    /* GetStr */
  172.  
  173. /*****************************************************************************
  174.  * Str2Int returns the integer equivilent of str.
  175.  ****************************************************************************/
  176. int Str2Int(s)
  177.     char    *s;    /* The string we are converting to an integer. */
  178. {    int    i;    /* The value returned by this routine. */
  179.  
  180.     for (i = 0; *s && isdigit(*s); s++)
  181.         i = i * 10 + *s - '0';
  182.     return(i);
  183.  
  184. }    /* Str2Int */
  185.  
  186. /*****************************************************************************
  187.  * Reverse copies 's' into 'r' in reverse order and returns 'r'.
  188.  ****************************************************************************/
  189. char *Reverse(s,r)
  190.     char    *s,    /* The string in forward order. */
  191.         *r;    /* The result in reverse order. */
  192. {    int    i,    /* A loop counter. */
  193.         l;    /* The length of 's'. */
  194.  
  195.     for (l = strlen(s), i = 0; i < l; i++)
  196.         r[i] = s[l - i - 1];
  197.     r[l] = '\0';
  198.     return(r);
  199.  
  200. }    /* Reverse */
  201.  
  202. /*****************************************************************************
  203.  * StrRcmp returns true if 's1' in reverse is contained at the begining of
  204.  * 's2' in reverse.  Otherwise it returns false.
  205.  * Example: say 's1' = ".some.site", and 's2' = "this.some.site", if you reverse
  206.  * both of these strings you get
  207.  * 's1' = "etis.emos."
  208.  * 's2' = "etis.emos.siht"  which evalutaes to true.  If 's1' is not contained
  209.  * in 's2' we return false.
  210.  * This routine was written to support the use of domains as hosts contained
  211.  * in 'hosts2search'.
  212.  ****************************************************************************/
  213. short StrRcmp(s1,s2)
  214.     char    *s1,    /* The string which must be in s2 (in reverse order). */
  215.         *s2;    /* The string which must contain s1. */
  216. {    char    r1[255],/* The string s1 in reverse. */
  217.         r2[255];/* The string s2 in reverse. */
  218.  
  219.     for (s1 = Reverse(s1,r1), s2 = Reverse(s2,r2); *s1 && *s2; s1++, s2++)
  220.         if (*s1 != *s2)
  221.             return(0);
  222.     if (*s1 && !*s2)
  223.         return(0);
  224.     return(1);
  225.             
  226. }    /* StrRcmp */
  227.  
  228.  /*VARARGS*/
  229.  
  230. /*****************************************************************************
  231.  * Mysprint takes a variable number of arguments similar to fprintf() or
  232.  * or vsprintf() inparticular, and puts the formated information into a string
  233.  * and returns the string.  This routine make use of the defines USE_VARARGS_H
  234.  * and VSPRINTF_RETURNS_STR.  If USE_VARARGS_H is defined we use <varargs.h>
  235.  * otherwise <stdarg.h> gets included and used.  If VSPRINTF_RETURNS_STR is
  236.  * define it is assumed the call to vsprintf() returns a char* otherwise it
  237.  * must return an int.
  238.  ****************************************************************************/
  239. #ifdef USE_VARARGS_H
  240.  char *Mysprint(va_alist)
  241.     va_dcl
  242. {
  243.     char        *format;    /* Format of the arguments. */
  244. #else /* We are using <stdarg.h>. */
  245.  char *Mysprint(char *format,...)
  246. {
  247. #endif /* Using <stdarg.h>. */
  248.     static char    str[1024];    /* The string we send to the system. */
  249. #ifdef VSPRINTF_RETURNS_STR
  250.     char        *result;    /* The result of calling vsprintf. */
  251. #else /* vsprintf() returns an int. */
  252.     int        result;        /* The result of calling vsprintf. */
  253. #endif /* vsprintf() returns an int. */
  254.     va_list        ap;        /* The arguments beyond 'format'. */
  255.  
  256. #ifdef USE_VARARGS_H
  257.     va_start(ap);
  258.     format = va_arg(ap,char *);
  259. #else  /* We are using <stdarg.h>. */
  260.     va_start(ap,format);
  261. #endif  /* Using <stdarg.h>. */
  262.  
  263.     result = vsprintf(str,format,ap);
  264.     va_end(ap);
  265.  
  266. #ifdef VSPRINTF_RETURNS_STR
  267.     if (strcmp((char *)result,str))
  268.         {
  269.         fprintf(stderr,"error: Mysprint had vsprintf fail returning a (char *).\n");
  270.         return((char *)0);
  271.         }
  272.     else
  273.         return((char *)result);
  274. #else /* vsprintf() returns an int. */
  275.     if (result < 0)
  276.         {
  277.         fprintf(stderr,"error: Mysprint had vsprintf fail returning an (int).\n");
  278.         return((char *)0);
  279.         }
  280.     else
  281.         return(str);
  282. #endif /* vsprintf() returns an int. */
  283.  
  284. }    /* Mysprint */
  285.  
  286. /*ARGSUSED*/
  287.  
  288. /*****************************************************************************
  289.  * DoSystemCall does a system on the result of 'format'.
  290.  ****************************************************************************/
  291.  int DoSystemCall(command)
  292.     char    *command;        /* The command to send to system(). */
  293. {    int    result,            /* The result of calling vsprintf. */
  294.         error = 0;        /* Did things turn out ok? */
  295.  
  296.     if (command)
  297.         if (result = system(command))
  298.             error = fprintf(stderr,"error: DoSystemCall [%s] failed with %d\n",command,result);
  299.  
  300.     return(error);
  301.  
  302. }    /* DoSystemCall */
  303.  
  304. /*****************************************************************************
  305.  * MyStrTok returns breaks 'str' into tokens as per the token seperator
  306.  * 'delimeter'.  This routine basicly does the samething as strtok().
  307.  ****************************************************************************/
  308. char *MyStrTok(str,delimeter)
  309.     char        *str,        /* The string to tokenize. */
  310.             delimeter;    /* The delimeter character. */
  311. {    static char    *theStr,    /* Keep a copy the string to tokenize. */
  312.             *s,        /* A temporary pointer. */
  313.             *token;        /* The token to return. */
  314.  
  315.     if (str)    /* A new string to tokenize. */
  316.         theStr = str;
  317.  
  318.     token = theStr;    /* This is the token. */
  319.  
  320.     /* Break up theStr into a token. */
  321.     if (*theStr != delimeter)
  322.         for (s = theStr; *s && *s != delimeter; s++)
  323.             ;
  324.  
  325.     /* Nill out the token and do some cleaning up. */
  326.     *s++ = '\0';
  327.     theStr = s;
  328.     return(token);
  329.  
  330. }    /* MyStrTok */
  331.  
  332. /*****************************************************************************
  333.  * OnlyDigits returns a null terminated string containing only the digit
  334.  * characters contained in 's'.  What we do is convert the first non-digit
  335.  * character in 's' to the null character and return the string.
  336.  ****************************************************************************/
  337. char *OnlyDigits(s)
  338.     char    *s;        /* The input string. */
  339. {    char    *digits;    /* The string with digits only. */
  340.  
  341.     for (digits = s; *s; s++)
  342.         if (!isdigit(*s))
  343.             {
  344.             *s = '\0';
  345.             return(digits);
  346.             }
  347.             
  348.     return(digits);
  349.  
  350. }    /* OnlyDigits */
  351.  
  352. /*****************************************************************************
  353.  * RemoveCRLF removes the carriage return or line feed by changing it to
  354.  * the null character.
  355.  ****************************************************************************/
  356. char *RemoveCRLF(line)
  357.     char    *line;        /* The line we are dealing with. */
  358. {    char    *s;        /* Pointer into 'line'. */
  359.  
  360.     for (s = line; *s && *s != '\r' && *s != '\n'; s++)
  361.         ;    /* Go to the CR or LF position. */
  362.     *s = '\0';
  363.     return(s);
  364.  
  365. }    /* RemoveCRLF */
  366.  
  367.